﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;
using System.Data.SqlClient;

namespace SMSRouter
{

    public partial class SummaryView : Form, IDisposable
    {
        public List<string> Log = new List<string>();
        public BindingList<Message> OutboxCashe = new BindingList<Message>();
        public BindingList<Message> InboxCashe = new BindingList<Message>();
        delegate void AddLogItemCallback(string text);
        delegate void ModifyInboxAndOutboxCallback();
        /*MessageStorageModel messageStorageModel;
        ThreadManager threadManager;*/
        Controller controller;
        public SummaryView()
        {
            InitializeComponent();
            controller = new Controller(this);
            /*messageStorageModel = new MessageStorageModel();
            threadManager = new ThreadManager(this, messageStorageModel);*/
            dataGridViewOutbox.DataSource = OutboxCashe;
            dataGridViewInbox.DataSource = InboxCashe;
        }

        public void AddLogMessage(string message)
        {
            if (this.listBoxLog.InvokeRequired)
            {
                AddLogItemCallback d = new AddLogItemCallback(AddLogMessage);
                this.Invoke(d, new object[] { message });
            }
            else
            {
                Log.Add(message);
                /*listBoxLog.Items.Clear();
                foreach (string str in Log)*/
                listBoxLog.Items.Insert(0, "["+ DateTime.Now.ToString()+ "]: "+message);
            }
        }

        public void ModifyInboxAndOutbox()
        {
            if (this.listBoxLog.InvokeRequired)
            {
                ModifyInboxAndOutboxCallback d = new ModifyInboxAndOutboxCallback(ModifyInboxAndOutbox);
                this.Invoke(d, new object[] { });
            }
            else
            {
                
                    bool alreadyExist = false;
                    foreach (Message mes in controller.MessagesList)
                    {
                        alreadyExist = false;
                        for (int i = 0; i < InboxCashe.Count; i++)
                            if ((mes.id == InboxCashe[i].id)&&(mes.folder == "Inbox"))
                            {
                                alreadyExist = true;
                                InboxCashe[i] = mes;
                            }
                        if ((!alreadyExist) &&(mes.folder == "Inbox"))
                            InboxCashe.Add(mes);

                        alreadyExist = false;
                        for (int i = 0; i < OutboxCashe.Count; i++)
                            if ((mes.id == OutboxCashe[i].id) && (mes.folder != "Inbox"))
                            {
                                alreadyExist = true;
                                OutboxCashe[i] = mes;
                            }
                        if ((!alreadyExist) && (mes.folder != "Inbox"))
                            OutboxCashe.Add(mes);
                    }
            }
        }



        private void buttonSend_Click(object sender, EventArgs e)
        {
            Message mes = new Message();
            mes.From = "Server";
            mes.To = textBoxNumber.Text;
            mes.text = textBoxMessageText.Text;
            mes.folder = "Outbox";
            controller.AddNewMessage(mes);
        }

        private void SummaryView_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        public void Dispose()
        {
            controller.Dispose();
        }

        private void modemSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ModemSettings modemSessings = new ModemSettings(controller);
            modemSessings.ShowDialog();
            if (controller.smsManager.modemOnline)
                toolStripStatusLabelModem.Text = "Modem online at " + controller.smsManager.port.PortName;
            else
                toolStripStatusLabelModem.Text = "Modem offline";
        }

        private void databaseConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataConnectionDialog dcd = new DataConnectionDialog();
            DataConnectionConfiguration dcs = new DataConnectionConfiguration(null);
            dcs.LoadConfiguration(dcd);

            if (DataConnectionDialog.Show(dcd) == DialogResult.OK)
            {
                controller.dbManager.connectionString = dcd.ConnectionString;
                if (controller.dbManager.TestConnection())
                {
                    toolStripStatusLabelDatabase.Text = "Database connected";
                    controller.Synchronize();
                }
                else
                    toolStripStatusLabelDatabase.Text = "Database disconnected";
            }

            dcs.SaveConfiguration(dcd);
        }
    }
}
